package com.cdd.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdd.index.mapper.BannerMapper;
import com.cdd.index.service.IBannerService;
import com.common.entity.Banner;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-05
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public CommonResult selectAll(Integer currentPage, Integer pageSize, Banner banner) {
        Page<Banner> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Banner> wrapper =new QueryWrapper<>();

        if (StringUtils.hasText(banner.getTitle())){
            wrapper.like("title",banner.getTitle());
        }
        if (banner.getIsDisable()!=null){
            wrapper.eq("is_disable",banner.getIsDisable());
        }
        Page<Banner> bannerPage = bannerMapper.selectPage(page, wrapper);
        //System.out.println(bannerPage.getRecords());

        return new CommonResult(2000,"查询成功",bannerPage);
    }

    @Override
    public CommonResult updateStatus(String id, String isDisable) {
        System.out.println("-------------------"+isDisable);
        Banner banner = new Banner();
        banner.setId(id);
        banner.setIsDisable(isDisable);
        int i = bannerMapper.updateById(banner);
        if(i!=0){
            return new CommonResult(2000,"修改状态成功",null);
        }

        return new CommonResult(5000,"修改状态失败",null);
    }

    @Override
    public CommonResult findAll() {
        QueryWrapper<Banner> wrapper =new QueryWrapper<>();
        wrapper.eq("is_disable",0);
        wrapper.eq("is_deleted",0);
        List<Banner> bannerList = bannerMapper.selectList(wrapper);
        return new CommonResult(2000,"查询成功",bannerList);
    }

    @Override
    public CommonResult bannerAdd(Banner banner) {
        banner.setIsDeleted(0);
        banner.setIsDisable("0");
        bannerMapper.insert(banner);
        return new CommonResult(2000,"添加成功",null);
    }

    @Override
    public CommonResult deleteBanner(String id) {
        bannerMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",null);
    }

    @Override
    public CommonResult updateBanner(Banner banner) {
        bannerMapper.updateById(banner);
        return new CommonResult(2000,"修改成功",null);
    }
}